<?php
/**
 * Created by PhpStorm.
 * User: hebidu
 * Date: 15/12/5
 * Time: 11:55
 */

namespace Shop\Api;


use Api\Vendor\NetWarehourse\WHOrderStatus;
use Common\Api\Api;
use Shop\Model\OrdersModel;
use Shop\Model\OrderStatusHistoryModel;

/**
 * 网仓状态更改统一操作
 * Class WMSOrderStatusApi
 * @package Shop\Api\
 */
class WMSOrderStatusApi extends Api  {


    /**
     * 9. 订单网仓状态 －》订单发货失败
     */
    const WMS_FAILED = "Shop/WMSOrderStatus/wms_failed";

    /**
     * 8. 订单网仓状态 －》接单失败
     */
    const WMS_REJECT = "Shop/WMSOrderStatus/wms_reject";

    /**
     * 7. 订单网仓状态 －》拣选状态
     */
    const WMS_PICK_LACK = "Shop/WMSOrderStatus/wms_pick_lack";

    /**
     * 6. 订单网仓状态 －》发货状态
     */
    const WMS_SHIPPED = "Shop/WMSOrderStatus/wms_shipped";


    /**
     * 5. 订单网仓状态 －》打包状态
     */
    const WMS_PACKAGE = "Shop/WMSOrderStatus/wms_package";

    /**
     * 4. 订单网仓状态 －》复核状态
     */
    const WMS_CHECK = "Shop/WMSOrderStatus/wms_check";

    /**
     * 3. 订单网仓状态 －》拣货状态
     */
    const WMS_PICK = "Shop/WMSOrderStatus/wms_pick";

    /**
     * 2. 订单网仓状态 －》接单状态
     */
    const WMS_ACCEPT = "Shop/WMSOrderStatus/wms_accept";

    /**
     * 1. 订单网仓状态 －》打印状态
     */
    const WMS_PRINT = "Shop/WMSOrderStatus/wms_print";
    /**
     * 1. 订单网仓状态 －》未上报状态
     */
    const WMS_INITIAL = "Shop/WMSOrderStatus/wms_initial";


    protected function _init(){

        $this->model = new OrdersModel();

    }

    /**
     * 9. 订单 未上报状态
     * @param $order_code
     * @return array
     */
    public function wms_initial($order_code){
        //2. 检测当前订单状态是否合法
        $next_status = WHOrderStatus::WMS_INITIAL;
        return $this->wms_common($order_code,$next_status);
    }


    /**
     * 9. 订单 拣选状态
     * @param $order_code
     * @return array
     */
    public function wms_failed($order_code){
        //2. 检测当前订单状态是否合法
        $next_status = WHOrderStatus::WMS_FAILED;
        return $this->wms_common($order_code,$next_status);
    }

    /**
     * 8. 订单 拣选状态
     * @param $order_code
     * @return array
     */
    public function wms_reject($order_code){
        //2. 检测当前订单状态是否合法
        $next_status = WHOrderStatus::WMS_REJECT;
        return $this->wms_common($order_code,$next_status);
    }

    /**
     * 7. 订单 拣选状态
     * @param $order_code
     * @return array
     */
    public function wms_pick_lack($order_code){
        //2. 检测当前订单状态是否合法
        $next_status = WHOrderStatus::WMS_PICK_LACK;
        return $this->wms_common($order_code,$next_status);
    }

    /**
     * 6. 订单 发货状态
     * @param $order_code
     * @return array
     */
    public function wms_shipped($order_code){
        //2. 检测当前订单状态是否合法
        $next_status = WHOrderStatus::WMS_SHIPED;
        return $this->wms_common($order_code,$next_status);
    }

    /**
     * 5. 订单 打包状态
     * @param $order_code
     * @return array
     */
    public function wms_package($order_code){
        //2. 检测当前订单状态是否合法
        $next_status = WHOrderStatus::WMS_PACKAGE;
        return $this->wms_common($order_code,$next_status);
    }

    /**
     * 4. 订单 复核状态
     * @param $order_code
     * @return array
     */
    public function wms_check($order_code){
        //2. 检测当前订单状态是否合法
        $next_status = WHOrderStatus::WMS_CHECK;
        return $this->wms_common($order_code,$next_status);
    }


    /**
     * 3. 订单拣货状态
     * @param $order_code
     * @return array
     */
    public function wms_pick($order_code){
        //2. 检测当前订单状态是否合法
        $next_status = WHOrderStatus::WMS_PICK;
        return $this->wms_common($order_code,$next_status);
    }

    /**
     * 2. 订单接单状态
     * @param $order_code
     * @return array
     */
    public function wms_accept($order_code){
        $next_status = WHOrderStatus::WMS_ACCEPT;
        return $this->wms_common($order_code,$next_status);
    }

    /**
     * 1. 订单打印状态
     * @param $order_code
     * @return array
     */
    public function wms_print($order_code){
        //2. 检测当前订单状态是否合法
        $next_status = WHOrderStatus::WMS_PRINT;
        $result = $this->wms_common($order_code,$next_status);

        return $result;
    }

    private function wms_common($order_code,$next_status){

        $result = $this->model->where(array('order_code'=>$order_code))->find();

        if($result == false){
            return $this->apiReturnErr($this->model->getDbError());
        }

        if(is_null($result)){
            return $this->apiReturnErr("订单ID错误!");
        }

        $cur_status = $result['wms_status'];
        $reason = WHOrderStatus::getDescription($next_status);
        if($cur_status == $next_status && $next_status == WHOrderStatus::WMS_ACCEPT){
            $reason = "重新".$reason;
        }

        if($next_status == WHOrderStatus::WMS_INITIAL){
            if($cur_status != WHOrderStatus::WMS_ACCEPT){
                return $this->apiReturnErr("订单状态不允许取消!");
            }
            $reason  = "取消订单";
        }

        $reason  = "仓库-".$reason;

        $entity = array(
            'reason'=>$reason,
            'order_code'=>$order_code,
            'operator'=>0,
            'status_type'=>strtoupper(OrderStatusHistoryModel::STATUS_TYPE_WMS),
            'isauto'=>1,
            'cur_status'=> $cur_status,
            'next_status'=> $next_status,
        );

        $orderStatusHistoryModel = new OrderStatusHistoryModel();
        $this->model->startTrans();
        $flag = true;
        $return = "";

        $result = $this->model->where(array('order_code'=>$order_code))->save(array('wms_status'=> $next_status ));

        if($result === false){
            $flag = false;
            $return = $this->model->getDbError();
        }

//        if($result == 0){
//            $flag = false;
//            $return = "订单ID有问题!";
//        }

        if($orderStatusHistoryModel->create($entity,1)){
            $result = $orderStatusHistoryModel->add();
            if($result === false){
                $flag = false;
                $return = $orderStatusHistoryModel->getDbError();
            }
        }else{
            $flag = false;
            $return = $orderStatusHistoryModel->getError();
        }

        if($flag){
            $this->model->commit();
            return $this->apiReturnSuc($return);
        }else{
            $this->model->rollback();
            return $this->apiReturnErr($return);
        }
    }

}